﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace VSM.Parts
{
    public partial class DepositActivitesList : System.Web.UI.Page
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session[Utils.AppConstants.ID_User] == null
                || Session[Utils.AppConstants.Password] == null
                || Session[Utils.AppConstants.ID_Dealer] == null)
            {
                Response.Redirect("Login.aspx");
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Timer1_Tick(object sender, EventArgs e)
        {
            LoadDeposit();
            Timer1.Enabled = false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAccept_Click(object sender, EventArgs e)
        {
            try
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "OK", "Main.okDialogClick('CloseForm');", true);
            }
            catch (Exception ex)
            {

            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdAva_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Attributes.Add("onmouseover", "MouseEvents(this, event)");
                e.Row.Attributes.Add("onmouseout", "MouseEvents(this, event)");
                e.Row.Attributes["style"] = "cursor:pointer";
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void LoadDeposit()
        {
            try
            {
                string dbType = Session[Utils.AppConstants.ID_Dealer].ToString() == "VSM" ? Utils.AppConstants.VSM : Utils.AppConstants.DEALER;
                GeneralFunctions GF = new GeneralFunctions(dbType, Session[Utils.AppConstants.ID_Dealer].ToString());

                MasterData obj = new MasterData();
                DataSet ds = obj.GetAllDepositActive(dbType, Session[Utils.AppConstants.U_Department].ToString());

                if (ds != null && ds.Tables.Count > 0)
                {
                    DataTable dt = new DataTable();
                    dt.Columns.Add("CardCode");
                    dt.Columns.Add("CardName");
                    dt.Columns.Add("DepositDate");
                    dt.Columns.Add("DepositAmt");
                    dt.Columns.Add("DepositLimit");
                    dt.Columns.Add("Remarks");

                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        DataRow row = dt.NewRow();
                        row["DepositDate"] = Convert.ToDateTime(dr["DepositDate"].ToString()).ToString("dd/MM/yyyy");
                        row["CardName"] = dr["CardName"].ToString();
                        row["CardCode"] = dr["CardCode"].ToString();
                        row["Remarks"] = dr["Remarks"].ToString();
                        row["DepositAmt"] = GF.FormatNumeric(dr["DepositAmt"].ToString(), "SumDec");
                        row["DepositLimit"] = GF.FormatNumeric(dr["DepositLimit"].ToString(), "SumDec");

                        dt.Rows.Add(row);
                    }

                    if (dt != null && dt.Rows.Count > 0)
                    {
                        BindBP(dt);
                    }
                    else
                    {
                        BindBP(null);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tb"></param>
        private void BindBP(DataTable tb)
        {
            Session["AvailableTable"] = tb;
            this.grdAva.DataSource = tb;
            this.grdAva.DataBind();
        }
    }
}
